/**
 * Created by anting on 16/11/15.
 */
const path = require("path");

var fs = require("fs");

var mkdirp = require("mkdirp");

function fileUpload(file, filePath) {

    var tempPath = file['path'];
    var filename = file['name'].split('.');
    var ext = '.' + filename[filename.length - 1];

    if (!fs.existsSync(filePath)) {
        mkdirp.sync(filePath);
    }
    var newFileName =  Date.parse(new Date()).toString() + ext;
    var filePath = path.join(filePath, newFileName);
    var stream = fs.createWriteStream(filePath);
    fs.createReadStream(tempPath).pipe(stream);

    return newFileName;
}

module.exports.fileUpload = fileUpload;

function formatDate(fmt) {
    var _this = new Date();
    var o = {
        "M+" : _this.getMonth()+1,                 //月份
        "d+" : _this.getDate(),                    //日
        "h+" : _this.getHours(),                   //小时
        "m+" : _this.getMinutes(),                 //分
        "s+" : _this.getSeconds(),                 //秒
        "q+" : Math.floor((_this.getMonth()+3)/3), //季度
        "S"  : _this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (_this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}
module.exports.formatDate = formatDate;


